<template>
  <div class="search">
    <NavBar title="面经搜索" path="/Home/sharepage"></NavBar>
    <!-- 搜索框 -->
    <div class="inputSearch">
      <van-search
        v-model="value"
        show-action
        shape="round"
        :clearable="false"
        placeholder="请输入搜索关键词"
        @search="onSearch(value)"
        @cancel="onCancel"
        @input="onInput"
      />
    </div>
    <div v-if="value == ''">
      <!-- 搜索热点 -->
      <div class="hot">
        <h4>大家都在搜</h4>
        <div class="searchHot">
          <span
            v-for="(item, index) in hotArr"
            :key="index"
            @click="hotBtn(item)"
            >{{ item }}</span
          >
        </div>
      </div>
      <!-- 历史记录 -->
      <div class="history">
        <div class="title">
          <div class="past">历史搜索</div>
          <div class="btn">清空</div>
        </div>
        <div class="footer">
          <span v-for="(item, index) in historyArr" :key="index">
            {{ item }}</span
          >
        </div>
      </div>
    </div>
    <div v-else>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        offset="10"
        :immediate-check="false"
      >
        <share
          :item="item"
          v-for="(item, index) in shareArr"
          :key="index"
        ></share>
      </van-list>
    </div>
  </div>
</template>
<script>
import { getshareHot, getshare } from '@/api/find.js'
import share from './share'
export default {
  components: { share },
  data () {
    return {
      value: '',
      hotArr: [],
      shareArr: [],
      historyArr: [],
      loading: false,
      finished: false,
      start: -5,
      limit: 5,
      total: ''
    }
  },
  created () {
    this.gethot()
    this.historyArr = JSON.parse(localStorage.getItem('heimamm-history')) || []
    console.log(this.historyArr)
  },
  methods: {
    // 热点搜索
    hotBtn (item) {
      this.value = item
    },
    // 获取热门搜索
    async gethot () {
      const res = await getshareHot()
      // console.log(res)
      this.hotArr = res.data
    },
    // 输入内容点击确定时触发
    async onSearch (value) {
      this.value = value
      this.shareArr = []
      // const res = await getshare()
      // console.log(res)
      if (this.value.trim().length == 0) {
        this.$toast.fail('搜索框内关键字不能为空')
        return
      }
      this.onLoad()
      this.historyArr.unshift(this.value)
      // 去重
      this.historyArr = [...new Set(this.historyArr)]
      this.historyArr.splice(6)
      localStorage.setItem('heimamm-history', JSON.stringify(this.historyArr))
    },
    //  取消按钮
    onCancel () {
      this.shareArr = []
    },
    // 输入框内容变化时触发
    onInput (value) {
      if (value === '') {
        this.shareArr = []
      }
    },
    // 下拉加载列表
    async onLoad () {
      this.start += this.limit
      const res = await getshare({
        satrt: this.satrt,
        limit: this.limit,
        q: this.value
      })
      this.total = res.data.total
      this.shareArr.push(...res.data.list)

      this.loading = false
      if (this.shareArr.length >= this.total) {
        this.finished = true
      }
    }
  }
}
</script>
<style lang="less" scoped>
.search {
  /deep/.van-hairline--bottom::after {
    border: 0;
  }
  /deep/.van-nav-bar__title {
    font-size: 18px;
  }
  /deep/.van-icon {
    color: #b5b6c0;
    font-size: 18px;
    font-weight: 600;
  }
  .inputSearch {
    padding-bottom: 10px;
    box-shadow: 0px 2px 4px 0px rgba(24, 26, 57, 0.04);
  }
  .hot {
    padding: 30px 15px;
    h4 {
      font-size: 22px;
      margin-bottom: 15px;
    }
    .searchHot {
      span {
        font-size: 14px;
        padding: 5px 10px;
        margin-right: 10px;
        border-radius: 5px;
        color: #545671;
        background-color: #f7f4f5;
      }
    }
  }
  .history {
    padding: 15px;
    .title {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .past {
      font-size: 22px;
      font-weight: bold;
      margin-bottom: 20px;
    }
    .btn {
      font-size: 16px;
      color: #545671;
    }
    .footer {
      display: flex;
      flex-wrap: wrap;
      span {
        font-size: 14px;
        padding: 5px 10px;
        margin-right: 8px;
        margin-bottom: 8px;
        border-radius: 5px;
        color: #545671;
        background-color: #f7f4f5;
      }
    }
  }
}
</style>
